package com.nineclock.approve.activiti;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.nineclock.approve.utils.ActivitiUtils;
import com.nineclock.common.utils.UUIDUtils;
import org.activiti.bpmn.model.SequenceFlow;
import org.activiti.bpmn.model.Process;

/**
 * 流程节点抽象类
 */
public abstract class ProcessNodeResolver {

    /**
     * 添加节点到流程Process中
     * @param process
     * @param currNode
     * @param nextNode
     */
    public abstract void addFlowNodeToActiviti(Process process, JSONObject currNode, JSONObject nextNode);

    /**
     * 获取当前节点的Key/ID
     * @param currNode
     * @return
     */
    protected String getNodeId(JSONObject currNode) {
        String type = currNode.getStr("type");
        String nodekey = currNode.getStr("nodeKey");
        return ActivitiUtils.getKey(type,nodekey);
    }

    /**
     * 创建流程图中的线条
     *
     * @param process 流程对象
     * @param currNode 当前节点
     * @param nextNode 下一个节点
     * @param cond 条件节点, 没有 , 直接传递 null
     * @return
     */
    protected SequenceFlow newSequenceFlow(Process process, JSONObject currNode, JSONObject nextNode){
        SequenceFlow sf = new SequenceFlow();
        sf.setId(ActivitiUtils.getKey("seq", UUIDUtils.getUUID()));
        sf.setSourceRef(getNodeId(currNode));
        sf.setTargetRef(getNodeId(nextNode));

        process.addFlowElement(sf);
        return sf;
    }
    
}